<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\Program as ProgramModule;
use app\common\facade\admin\System as SystemModule;
use app\common\facade\admin\ChannelInfo;
use think\exception\ValidateException;

class Program extends BaseController
{

    // 节目时段列表
    public function pgmseqList() {
        $param = $this->request->get([
            'channel_id' => $this->request->get('channel_id')?:null,
            'seg_name' => $this->request->get('seg_name')?:null,
            'offset' => $this->request->get('page/d')?:0,
            'length' => $this->request->get('size/d')?:20
        ]);

        try {
            $list = ProgramModule::pgmseqList($param);
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($list);
    }

    /* 节目时段添加 */
    public function pgmseqAdd() {
        $param = $this->request->post([
            'seg_name',
            'channel_id',
            's_time',
            'e_time'
        ]);

        // if(isset($param['pgm_seq_id'])) { // 添加
        //     unset($param['pgm_seq_id']);
        // }

        if(isset($param['s_time'])) {
            // 换算成秒数
            $s_time = explode(":", $param['s_time']);
            $param['s_time'] = $s_time[0] * 3600 + $s_time[1] * 60 + $s_time[2];
        }
        if(isset($param['e_time'])) {
            // 换算成秒数
            $e_time = explode(":", $param['e_time']);
            $param['e_time'] = $e_time[0] * 3600 + $e_time[1] * 60 + $e_time[2];
        }

        // 开始时间不能大于等于结束时间
        if ($param['s_time'] >= $param['e_time']) {
            return validateJson([],"开始时间不能晚于结束时间，请重新选择");
        }

        // 计算新增开始结束时间，是否与其他时间冲突
        $list = ProgramModule::pgmseqBetween($param['pgm_seq_id'],$param['s_time'],$param['e_time'],$param['channel_id']);
        if (!empty($list[0])) {
            return validateJson([],"此时段的时间与其他时段冲突，请重新选择");
        }
    
        try {
            // $this->validate($param, 'app\common\validate\Material.scenecopysave');
            $data = ProgramModule::pgmseqEdit($param);
        } catch (ValidateException $e) {
          return validateJson([], $e->getError());
        }  catch(\Exception $e) {
          return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);        
    }

    /* 节目时段编辑 */
    public function pgmseqEdit() {
        $param = $this->request->put([
            'pgm_seq_id',
            'seg_name',
            'channel_id',
            's_time',
            'e_time'
        ]);
        
        if(isset($param['s_time'])) {
            // 换算成秒数
            $s_time = explode(":", $param['s_time']);
            $param['s_time'] = $s_time[0] * 3600 + $s_time[1] * 60 + $s_time[2];
        }
        if(isset($param['e_time'])) {
            // 换算成秒数
            $e_time = explode(":", $param['e_time']);
            $param['e_time'] = $e_time[0] * 3600 + $e_time[1] * 60 + $e_time[2];
        }

        // 开始时间不能大于等于结束时间
        if ($param['s_time'] >= $param['e_time']) {
            return validateJson([],"开始时间不能晚于结束时间，请重新选择");
        }

        // 计算新增开始结束时间，是否与其他时间冲突
        $list = ProgramModule::pgmseqBetween($param['pgm_seq_id'],$param['s_time'],$param['e_time'],$param['channel_id']);
        if (!empty($list[0])) {
            return validateJson([],"此时段的时间与其他时段冲突，请重新选择");
        }

        try {
            $result = ProgramModule::pgmseqEdit($param);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }
        return successJson($result);
    }

    /**
     * 节目时段删除
     */
    public function pgmseqDel() {
        // 接收删除的id数组
        $ids = $this->request->delete();
        $id_str = implode(',', $ids);
        try {
            $res = ProgramModule::pgmseqDel($id_str);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($res);
    }

    // 广告时段列表
    public function advseqList() {
        $param = $this->request->get([
            'channel_id' => $this->request->get('channel_id')?:null,
            'adv_seg_name' => $this->request->get('adv_seg_name')?:null,
            'time_status' => $this->request->get('time_status')?:null,
            'offset' => $this->request->get('page/d')?:0,
            'length' => $this->request->get('size/d')?:20
        ]);

        try {
            $list = ProgramModule::advseqList($param);
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($list);
    }

    /* 广告时段添加 */
    public function advseqAdd() {
        $param = $this->request->post([
            'adv_seq_id',
            'adv_seg_name',
            'channel_id',
            's_time',
            'pgm_len',
            'week_use',
            's_date',
            'e_date',
            'remark',
            'rights_limit',
            'date_limit',
            'oper_users',
            'adv_segid'
        ]);

        if(isset($param['adv_seq_id'])) { // 添加
            unset($param['adv_seq_id']);
        }

        // 过滤周几可用
        if (isset($param['week_use'])) {
            $str = '';
            foreach ($param['week_use'] as $k => $val) {
                if ($val == '周一') { $str .= '1'; }
                if ($val == '周二') { $str .= '2'; }
                if ($val == '周三') { $str .= '3'; }
                if ($val == '周四') { $str .= '4'; }
                if ($val == '周五') { $str .= '5'; }
                if ($val == '周六') { $str .= '6'; }
                if ($val == '周日') { $str .= '7'; }
            }
            $param['week_use'] = $str;
        }

        // 有效时间转换
        if (isset($param['date_limit'])) {
            $param['date_limit'] = $param['date_limit'] ? 2 : 1;
        }

        // 可操作性用户权限转换
        if ($param['rights_limit']) {
            $param['oper_users'] = implode(",", $param['oper_users']);
        }

        // 权限控制转换
        if (isset($param['rights_limit'])) {
            $param['rights_limit'] = $param['rights_limit'] ? 2 : 1;
        }

        if(isset($param['s_time'])) {
            // 换算成秒数
            $s_time = explode(":", $param['s_time']);
            $param['s_time'] = ($s_time[0] * 3600 + $s_time[1] * 60 + $s_time[2])*25;
        }
        if(isset($param['pgm_len'])) {
            // 换算成秒数
            $pgm_len = explode(":", $param['pgm_len']);
            $param['pgm_len'] = ($pgm_len[0] * 3600 + $pgm_len[1] * 60 + $pgm_len[2])*25;
        }
        
        // 开始时间不能大于等于结束时间
        if ($param['s_date'] >= $param['e_date']) {
            return validateJson([],"开始时间不能晚于结束时间，请重新选择");
        }

        try {
          // $this->validate($param, 'app\common\validate\Material.scenecopysave');
          $data = ProgramModule::advseqEdit($param);
        } catch (ValidateException $e) {
          return validateJson([], $e->getError());
        }  catch(\Exception $e) {
          return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);        
    }

    /* 广告时段编辑 */
    public function advseqEdit() {
        $param = $this->request->put([
            'adv_seq_id',
            'adv_seg_name',
            'channel_id',
            's_time',
            'pgm_len',
            'week_use',
            's_date',
            'e_date',
            'remark',
            'rights_limit_s',
            'date_limit_s',
            'oper_users',
            'adv_segid'
        ]);

        // 过滤周几可用
        if (isset($param['week_use'])) {
            $str = '';
            foreach ($param['week_use'] as $k => $val) {
                if ($val == '周一') { $str .= '1'; }
                if ($val == '周二') { $str .= '2'; }
                if ($val == '周三') { $str .= '3'; }
                if ($val == '周四') { $str .= '4'; }
                if ($val == '周五') { $str .= '5'; }
                if ($val == '周六') { $str .= '6'; }
                if ($val == '周日') { $str .= '7'; }
            }
            $param['week_use'] = $str;
        }

        // 有效时间转换
        if (isset($param['date_limit_s'])) {
            $param['date_limit'] = $param['date_limit_s'] ? 2 : 1;
        }

        // 可操作性用户权限转换
        if ($param['rights_limit_s']) {
            $param['oper_users'] = implode(",", $param['oper_users']);
        }else{
            $param['oper_users'] = null;
        }

        // 权限控制转换
        if (isset($param['rights_limit_s'])) {
            $param['rights_limit'] = $param['rights_limit_s'] ? 2 : 1;
        }

        if(isset($param['s_time'])) {
            // 换算成秒数
            $s_time = explode(":", $param['s_time']);
            $param['s_time'] = ($s_time[0] * 3600 + $s_time[1] * 60 + $s_time[2])*25;
        }
        if(isset($param['pgm_len'])) {
            // 先取出可能存在的符号“-”
            $pgm_arr = explode("-", $param['pgm_len']);
            // 换算成秒数
            $pgm_len = explode(":", $pgm_arr[0]);
            $param['pgm_len'] = ($pgm_len[0] * 3600 + $pgm_len[1] * 60 + $pgm_len[2])*25;
        }
        
        // 开始时间不能大于等于结束时间
        if ($param['s_date'] >= $param['e_date']) {
            return validateJson([],"开始时间不能晚于结束时间，请重新选择");
        }
        unset($param['rights_limit_s']);
        unset($param['date_limit_s']);

        try {
            $result = ProgramModule::advseqEdit($param);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }
        return successJson($result);
    }

    /**
     * 节目时段删除
     */
    public function advseqDel() {
        // 接收删除的id数组
        $ids = $this->request->delete();
        $id_str = implode(',', $ids);
        try {
            $res = ProgramModule::advseqDel($id_str);
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($res);
    }

    // 节目单基本设置信息
    public function otherinfo() {
        try {
            $baseinfo = SystemModule::baseinfo();
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($baseinfo);
    }

    public function otherset() {
        $param = $this->request->post();
        try {
            $data = SystemModule::baseseting($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 获取频道列表信息
     */
    public function getChannelList(){
        $param = $this->request->get([
            'per_page' => $this->request->get('pagenum/d')?:1,
            'pagesize' => $this->request->get('pagesize/d')?:100,
        ]);
        $param['offset'] = ($param['per_page']-1) * $param['pagesize'];
        $list = ChannelInfo::channelList($param);
        return successJson($list, '获取成功');
    }
}